
export class ToolHandle {
    /**
     * 将json数据转换为树结构数据，并返回
     * @param {*} json json数据
     */
    static jsonToTreeData(json) {
        let treeData = [{
            id: json.id,
            label: '',
            value: '',
            root: true,
            type: json.type,
            title: json.title,
            description: json.description,
            properties: []
        }];

        if (json.properties) {
            Object.keys(json).forEach(item => {
                if (!['type', 'title', 'description', 'properties', 'id'].includes(item)) {
                    treeData[0][item] = json[item]
                }
            })
            jsonToArray(json.properties, treeData[0].properties)
        }

        function jsonToArray(jsonObj, treeList) {
            Object.keys(jsonObj).forEach((item, index) => {
                if (!jsonObj[item].properties) {
                    if (jsonObj[item]['items']) {
                        let obj = {
                            id: jsonObj[item].id,
                            label: item,
                            value: item,
                            type: jsonObj[item].type,
                            title: jsonObj[item].title,
                            description: jsonObj[item].description,
                            properties: []
                        }
                        Object.keys(jsonObj[item]).forEach(ele => {
                            if (!['type', 'id', 'items'].includes(ele)) {
                                obj[ele] = jsonObj[item][ele]
                            }
                        })
                        let subObj = {
                            id: jsonObj[item]['items']['id'],
                            type: jsonObj[item]['items']['type'],
                            isArrayItem: true
                        };
                        Object.keys(jsonObj[item]['items']).forEach(ele => {
                            if (!['type', 'id'].includes(ele)) {
                                subObj[ele] = jsonObj[item]['items'][ele]
                            }
                        })
                        treeList.push({
                            ...obj,
                            properties: [subObj]
                        });
                    } else {
                        let obj = {
                            id: jsonObj[item].id,
                            label: item,
                            value: item,
                            type: jsonObj[item].type,
                            title: jsonObj[item].title,
                            description: jsonObj[item].description,
                            properties: []
                        }
                        Object.keys(jsonObj[item]).forEach(ele => {
                            if (!['type', 'description', 'title', 'id'].includes(ele)) {
                                obj[ele] = jsonObj[item][ele]
                            }
                        })
                        treeList.push(obj)
                    }
                } else {
                    let obj = {
                        id: jsonObj[item].id,
                        label: item,
                        value: item,
                        type: jsonObj[item].type,
                        title: jsonObj[item].title,
                        description: jsonObj[item].description,
                        properties: []
                    }
                    Object.keys(jsonObj[item]).forEach(ele => {
                        if (!['type', 'title', 'description', 'properties'].includes(ele)) {
                            obj[ele] = jsonObj[item][ele]
                        }
                    })
                    treeList.push(obj);
                    jsonToArray(jsonObj[item].properties, treeList[index].properties)
                }
            })
        }
        return treeData;
    }
    /**
     * 获取当前新增节点的父id以及父节点
     * @param {*} nodeArr 树结构数据
     * @param {*} parentId 父节点id
     * @returns 
     */
    static recurNodeForParent(nodeArr, parentId) {
        let length = 0;
        let parentNode = null;
        nodeArr.forEach((item) => {
            if (item.children && item.children.length > 0) {
                if (item.id == parentId) {
                    length = item.children.length;
                    parentNode = item;
                } else {
                    let result = this.recurNodeForParent(item.children, parentId);
                    if (result) {
                        length = result.length;
                        parentNode = result.parentNode;
                    }
                }
            }
        });
        return { length, parentNode };
    }
    /**
     * 添加树节点
     * @param {*} nodeArr 树结构数据
     * @param {*} node 添加节点要依据的节点数据
     * @param {*} addType 添加的节点的类型，为数组的子节点还是普通的节点
     * @param {*} insert  是否是子节点 子节点/相邻节点
     */
    static addNode(nodeArr, node, addType, insert = false) {
        switch (addType) {
            case 'arrayItem':
                // 给类型为array的节点添加子节点
                if (!node.children) {
                    this.$set(node, 'children', []);
                }
                const nodeId = node.id * 10 + node.children.length + 1;
                const newChild = {
                    id: nodeId,
                    label: "字段名称",
                    data_type: "string",
                    allow_null: true,
                    isNecessary: false,
                    isArrayItem: true
                };
                node.children.push(newChild);
                break;
            case 'node':
                const id = insert
                    ? node.id * 10 + node.children.length + 1
                    : node.parentId == -1 ? nodeArr.length + 1 : node.parentId * 10 +
                        this.recurNodeForParent(nodeArr, node.parentId).length +
                        1;
                const newChildNode = {
                    id: id,
                    label: "字段名称",
                    data_type: "string",
                    allow_null: true,
                    isNecessary: false
                };
                if (insert) {
                    node.children.push(newChildNode);
                } else {
                    if (node.parentId == -1) {
                        nodeArr.push(newChildNode);
                    } else {
                        this.recurNodeForParent(nodeArr, node.parentId)
                            .parentNode.children.push(newChildNode);
                    }

                }
                break;

        }
    }
    /**
     * 
     * @param {*} zhData 中文
     * @param {*} enData 英文
     * @param {*} language 当前语言
     * @param {*} param 参数
     * @returns 
     */
    static getI18nName(zhData, enData, language, param) {
        if (language == 'zh') {
            return zhData[param]
        } else {
            return enData[param]
        }
    }
} 